library(plyr)
library(dplyr)
library(tidyverse)
library(gsynth)
library(stargazer)
library(modelsummary) # https://modelsummary.com/articles/modelsummary.html#new-models-and-custom-statistics

setwd("~/Dropbox/China Huawei Paper/Production Materials/Replication/")
master.data <- read.csv("dataFile_China_Huawei.csv"); head(master.data)


# 250k
dat <- master.data
dat$treated = 0
threshold=250000
dat = as.data.frame(dat %>%
                      group_by(cabb) %>%
                      dplyr::mutate(treated = ifelse(row_number() == 1, 0, NA),
                                    treated = ifelse(china.huawei > threshold, 1, treated)) %>%
                      fill(treated))
dat$treated[is.na(dat$treated)==T] <- 0

sub = dat[dat$v2x_polyarchy>0.42,]
sub = sub[sub$cabb!="MNG",]
sub = sub[sub$cabb!="SSD",]
sub = sub[is.na(sub$v2smgovfilprc)==F & is.na(sub$treated)==F & is.na(sub$v2x_polyarchy)==F & is.na(sub$pres.election)==F & is.na(sub$coup.attempts)==F & is.na(sub$successful.coups)==F,]
sub = sub[is.na(sub$pcGDPcur)==F & is.na(sub$GDPcur)==F & is.na(sub$electricitypc)==F,]
sub = sub[is.na(sub$acled.protests)==F,]
nrow(dat);nrow(sub)

# had to drop to min.T0=3
out1 <- gsynth(icews.protest ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=3, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
out1$est.avg
out1est250 = out1$est.avg

# 500k
dat <- master.data
dat$treated = 0
threshold=500000
dat = as.data.frame(dat %>%
                      group_by(cabb) %>%
                      dplyr::mutate(treated = ifelse(row_number() == 1, 0, NA),
                                    treated = ifelse(china.huawei > threshold, 1, treated)) %>%
                      fill(treated))
dat$treated[is.na(dat$treated)==T] <- 0

sub = dat[dat$v2x_polyarchy>0.42,]
sub = sub[sub$cabb!="MNG",]
sub = sub[sub$cabb!="SSD",]
sub = sub[is.na(sub$v2smgovfilprc)==F & is.na(sub$treated)==F & is.na(sub$v2x_polyarchy)==F & is.na(sub$pres.election)==F & is.na(sub$coup.attempts)==F & is.na(sub$successful.coups)==F,]
sub = sub[is.na(sub$pcGDPcur)==F & is.na(sub$GDPcur)==F & is.na(sub$electricitypc)==F,]
nrow(dat);nrow(sub)

# min.T0=5
out1 <- gsynth(icews.protest ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=5, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
out1$est.avg
out1est500 = out1$est.avg


# 1 million
dat <- master.data
dat$treated = 0
threshold=1000000
dat = as.data.frame(dat %>%
                      group_by(cabb) %>%
                      dplyr::mutate(treated = ifelse(row_number() == 1, 0, NA),
                                    treated = ifelse(china.huawei > threshold, 1, treated)) %>%
                      fill(treated))
dat$treated[is.na(dat$treated)==T] <- 0

sub = dat[dat$v2x_polyarchy>0.42,]
sub = sub[sub$cabb!="MNG",]
sub = sub[sub$cabb!="SSD",]
sub = sub[is.na(sub$v2smgovfilprc)==F & is.na(sub$treated)==F & is.na(sub$v2x_polyarchy)==F & is.na(sub$pres.election)==F & is.na(sub$coup.attempts)==F & is.na(sub$successful.coups)==F,]
sub = sub[is.na(sub$pcGDPcur)==F & is.na(sub$GDPcur)==F & is.na(sub$electricitypc)==F,]
nrow(dat);nrow(sub)

out1 <- gsynth(icews.protest ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=5, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
out1$est.avg
out1est1m = out1$est.avg


# 5 million
dat <- master.data
dat$treated = 0
threshold=5000000
dat = as.data.frame(dat %>%
                      group_by(cabb) %>%
                      dplyr::mutate(treated = ifelse(row_number() == 1, 0, NA),
                                    treated = ifelse(china.huawei > threshold, 1, treated)) %>%
                      fill(treated))
dat$treated[is.na(dat$treated)==T] <- 0

sub = dat[dat$v2x_polyarchy>0.42,]
sub = sub[sub$cabb!="MNG",]
sub = sub[sub$cabb!="SSD",]
sub = sub[is.na(sub$v2smgovfilprc)==F & is.na(sub$treated)==F & is.na(sub$v2x_polyarchy)==F & is.na(sub$pres.election)==F & is.na(sub$coup.attempts)==F & is.na(sub$successful.coups)==F,]
sub = sub[is.na(sub$pcGDPcur)==F & is.na(sub$GDPcur)==F & is.na(sub$electricitypc)==F,]
nrow(dat);nrow(sub)

out1 <- gsynth(icews.protest ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=5, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
out1$est.avg
out1est5m = out1$est.avg


# 10 million
dat <- master.data
dat$treated = 0
threshold=10000000
dat = as.data.frame(dat %>%
                      group_by(cabb) %>%
                      dplyr::mutate(treated = ifelse(row_number() == 1, 0, NA),
                                    treated = ifelse(china.huawei > threshold, 1, treated)) %>%
                      fill(treated))
dat$treated[is.na(dat$treated)==T] <- 0

sub = dat[dat$v2x_polyarchy>0.42,]
sub = sub[sub$cabb!="MNG",]
sub = sub[sub$cabb!="SSD",]
sub = sub[is.na(sub$v2smgovfilprc)==F & is.na(sub$treated)==F & is.na(sub$v2x_polyarchy)==F & is.na(sub$pres.election)==F & is.na(sub$coup.attempts)==F & is.na(sub$successful.coups)==F,]
sub = sub[is.na(sub$pcGDPcur)==F & is.na(sub$GDPcur)==F & is.na(sub$electricitypc)==F,]
nrow(dat);nrow(sub)

out1 <- gsynth(icews.protest ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.repression + GDPcur + pcGDPcur + electricitypc, min.T0=5, data=sub, index=c("cabb","year"), estimator = "mc", se=TRUE, nboots=1000, seed=02139)
out1$est.avg
out1est10m = out1$est.avg


# export table
ti = data.frame(rbind(out1est250,out1est500,out1est1m,out1est5m,out1est10m))
ti$term = c("Transfer \u2265 $250,000","Transfer \u2265 $500,000","Transfer \u2265 $1,000,000","Transfer \u2265 $5,000,000","Transfer \u2265 $10,000,000")
ti = ti[,c("term","Estimate","S.E.","p.value")]
names(ti) = c("term","estimate","std.error","p.value")
out1 = list(tidy=ti)
class(out1) = "modelsummary_list"


myrows = data.frame(list(c("Country Fixed Effects",rep("Yes",1))
                         ,c("Year Fixed Effects",rep("Yes",1))
                         ,c("Control Variables",rep("Yes",1))
))

export = modelsummary(list("Protests" = out1
)
,add_rows=data.frame(t(myrows))
, stars=c('*' = .1, '**' = 0.05, '***' = .01)
, output="latex_tabular"
)
export = gsub("≥", "$\\\\ge$", export)
export




